﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;

namespace Q.Cms
{
    // Note: For instructions on enabling IIS6 or IIS7 classic mode, 
    // visit http://go.microsoft.com/?LinkId=9394801

    public class MvcApplication : System.Web.HttpApplication
    {
        public override string GetVaryByCustomString(HttpContext context, string custom)
        {
            return context.Request.Headers["cookie"];
            //return base.GetVaryByCustomString(context, custom);
        }

        public static void RegisterGlobalFilters(GlobalFilterCollection filters)
        {
            filters.Add(new HandleErrorAttribute());
            //filters.Add(new filerander());
        }

        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            routes.MapRoute(
                "Default", // Route name
                "{controller}/{action}.html", // URL with parameters
                new { controller = "home", action = "index" } // Parameter defaults
            );

            routes.MapRoute(
                "base", // Route name
                "{controller}/{action}/{id}", // URL with parameters
                new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
            );
            //ControllerBuilder.Current.SetControllerFactory(new fa());
            
        }

        

        protected void Application_Error(object sender, EventArgs e)
        {
            HttpContext context = HttpContext.Current;
            if (context.IsCustomErrorEnabled)
            {

            }
        }

        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();
            
            RegisterGlobalFilters(GlobalFilters.Filters);
            RegisterRoutes(RouteTable.Routes);
            
        }


        class filerander : IResultFilter
        {

            public void OnResultExecuted(ResultExecutedContext filterContext)
            {
                
            }

            public void OnResultExecuting(ResultExecutingContext filterContext)
            {
                filterContext.HttpContext.Response.Filter = new ResponseFilterWrapper(filterContext.HttpContext.Response.Filter);
                return;
            }
        }

        class ResponseFilterWrapper : System.IO.Stream
        {

            private System.IO.Stream inner;

            public ResponseFilterWrapper(System.IO.Stream s)
            {

                inner = s;
            }


            public override bool CanRead
            {
                get { return inner.CanRead; }
            }

            public override bool CanSeek
            {
                get { return inner.CanSeek; }
            }

            public override bool CanWrite
            {
                get { return inner.CanWrite; }
            }

            public override void Flush()
            {
                inner.Flush();
            }

            public override long Length
            {
                get { return inner.Length; }
            }

            public override long Position
            {
                get
                {
                    return inner.Position;
                }
                set
                {
                    inner.Position = value;
                }
            }

            public override int Read(byte[] buffer, int offset, int count)
            {
                return inner.Read(buffer, offset, count);
            }

            public override long Seek(long offset, System.IO.SeekOrigin origin)
            {
                return inner.Seek(offset, origin);
            }

            public override void SetLength(long value)
            {
                inner.SetLength(value);
            }

            public override void Write(byte[] buffer, int offset, int count)
            {
                inner.Write(buffer, offset, count);
                string p = HttpContext.Current.Server.MapPath(HttpContext.Current.Request.Path);
                //System.IO.StreamReader reader = new System.IO.StreamReader(HttpContext.Current.Response.Filter);
                System.IO.File.WriteAllText(p, System.Text.Encoding.UTF8.GetString(buffer));
            }
        }

        public class fa : DefaultControllerFactory
        {
            protected override IController GetControllerInstance(RequestContext requestContext, Type controllerType)
            {
                return base.GetControllerInstance(requestContext, controllerType);
            }
        }
    }
}